<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="部门" prop="deptId">
        <el-cascader
          :props="{ value: 'id', checkStrictly: true }"
          :show-all-levels="false"
          placeholder="请选择部门"
          v-model="queryParams.deptId"
          :options="deptOptions"
        ></el-cascader>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manageAudit:correct:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manageAudit:correct:edit']"
        >修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manageAudit:correct:remove']"
          >删除</el-button
        >
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manageAudit:correct:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="printBox"
          v-hasPermi="['manageAudit:correct:print']"
        >打印</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      @selection-change="handleSelectionChange"
      fit
      v-loading="loading"
      border
      :data="dataList"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column min-width="200" show-overflow-tooltip label="年度">
        <template slot-scope="scope">
          <span>
            {{
            scope.row.innerManagerAudit.auditYear
            }}年度管理评审工作计划
          </span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="责任部门" min-width="150" prop="deptName" />
      <el-table-column :show-overflow-tooltip="true" label="发出日期" min-width="150" prop="sendDate" />
      <el-table-column
        :show-overflow-tooltip="true"
        label="完成日期"
        min-width="150"
        prop="finishDate"
      />
      <el-table-column :show-overflow-tooltip="true" label="内容摘要" prop="content" min-width="150" />
      <el-table-column :show-overflow-tooltip="true" label="验证人" prop="checkUser" min-width="150" />
      <el-table-column
        :show-overflow-tooltip="true"
        label="验证结果及完成时间"
        prop="result"
        min-width="150"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['manageAudit:correct:info']"
            @click="handleDetail(scope.row)"
          >详情</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manageAudit:correct:edit']"
          >补充信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manageAudit:correct:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 打印模板位置 -->
    <PrintBox :checkNum="9" :printRow="printList" :isVertical="false" id="printBox">
      <template slot="title" slot-scope="scope">
        <div style="margin-bottom: 10px">改进、纠正和预防措施实施情况一览表 STET/B/39-2</div>
      </template>
      <template slot-scope="scope">
        <!-- 表格  class  table-page  必须-->
        <table
          style="border-collapse: collapse; table-layout: fixed"
          border="1"
          cellspacing="0"
          class="table-page"
        >
          <tr>
            <td style="width: 30mm">改进、纠正和预防措施实施序号</td>
            <td style="width: 40mm">内容摘要</td>
            <td style="width: 30mm">责任部门</td>
            <td style="width: 45mm">发出日期</td>
            <td style="width: 40mm">完成日期</td>
            <td style="width: 40mm">验证人</td>
            <td style="width: 40mm">验证结果及完成时间</td>
            <td style="width: 40mm">备注</td>
          </tr>
          <tr v-for="(e, i) in 10">
            <td>{{ scope.item.contentList[i] && i + 1 }}</td>

            <td>
              {{
              scope.item.contentList[i] && scope.item.contentList[i].content
              }}
            </td>
            <td>
              {{
              scope.item.contentList[i] && scope.item.contentList[i].deptName
              }}
            </td>
            <td>
              {{
              scope.item.contentList[i] && scope.item.contentList[i].sendDate
              }}
            </td>
            <td>
              {{
              scope.item.contentList[i] &&
              scope.item.contentList[i].finishDate
              }}
            </td>
            <td>
              {{
              scope.item.contentList[i] && scope.item.contentList[i].checkUser
              }}
            </td>
            <td>
              {{
              scope.item.contentList[i] && scope.item.contentList[i].result
              }}
            </td>
            <td>
              {{
              scope.item.contentList[i] && scope.item.contentList[i].remark
              }}
            </td>
          </tr>
        </table>
        <!-- 底部信息位置 -->

        <div class="page-bottom" style="page-break-after: always">
          <div class="desc">注：本表格自2022年5月1日起生效</div>
        </div>
      </template>
    </PrintBox>
  </div>
</template>

<script>
import {
  listmanagerOptimize,
  delmanagerOptimize
} from "@/api/quality/manageAudit/managerOptimize.js";
import { userSelect, deptTreeSelect } from "@/api/system/user.js";
import printStyle from "@/utils/print.js";

export default {
  name: "managerOptimize",
  dicts: ["sys_comp_fee_status"],

  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        feeNo: undefined,
        feeName: undefined,
        projectName: undefined,
        deptId: []
      },
      // 日期范围
      dateRange: [],

      userList: [],
      deptOptions: [],
      infos: [], //勾选的数据
      printList: []
    };
  },
  created() {
    const str = sessionStorage.getItem("queryParams");
    if (str) {
      this.queryParams = JSON.parse(str);
      setTimeout(() => {
        sessionStorage.removeItem("queryParams");
      }, 500);
    }
    this.getList();
    this.getDeptTree();
  },
  methods: {
    printBox() {
      //infos  勾选的数据 如果有勾选就打印勾选。
      if (this.infos.length) {
        this.printList = [
          {
            contentList: this.infos
          }
        ];
      } else {
        //反之打印当前整个列表
        this.printList = [
          {
            contentList: this.dataList
          }
        ];
      }
      setTimeout(() => {
        document.title = "改进、纠正和预防措施实施情况一览表 STET/B/39-2";
        this.$print({
          printable: "printBox", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyle(),
          onPrintDialogClose: () => {
            document.title = process.env.VUE_APP_TITLE;
          }
        });
      }, 500);
    },
    // 可以自定义样式  printStyle 是公共样式 切勿私自修改
    workStyle() {
      return `${printStyle}
        table,
        table tr th,
        table tr td {
          height:15mm;
          padding: 0px;
          font-size: 12px;
        }
        .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
            .df {
                display: flex;
              }
      `;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.innerManagerOptimizeId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.infos = selection;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem("queryParams", JSON.stringify(this.queryParams));

      const configId = row.innerManagerOptimizeId || this.ids;
      this.$router.push("./addCorrect?iszzlist=1&isEdit=true&id=" + configId);
    },
    handleDetail(row) {
      sessionStorage.setItem("queryParams", JSON.stringify(this.queryParams));

      this.$router.push(
        "./addCorrect?isInfo=true&id=" + row.innerManagerOptimizeId
      );
      // this.$router.push({
      //   path: "/managecorrect/process/detail/" + row.piid,
      //   query: {
      //     definitionId: null,
      //     deployId: null,
      //     taskId: null,
      //     finished: false,
      //     id: row.innerManagerOptimizeId,
      //   },
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.innerManagerOptimizeId || this.ids;
      this.$modal
        .confirm(
          "是否确认删除" +
            row.innerManagerAudit.auditYear +
            "年度管理评审工作计划的数据项？"
        )
        .then(function() {
          return delmanagerOptimize(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd() {
      this.$router.push("./addCorrect");
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    userSelect() {
      userSelect().then(response => {
        this.userList = response.data;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listmanagerOptimize({
        ...this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startReturnTime",
          "endReturnTime"
        ),
        deptId: this.queryParams.deptId.slice(-1)[0]
      }).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport() {
      this.download(
        "system/managerOptimize/export",
        {
          ...this.queryParams
        },
        `管理纠错记录表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
